package com.burgeon.backend.SHOPBACKEND.rest.marketing;

import com.burgeon.backend.Rest.base.RESTBase;

import com.burgeon.backend.SHOPBACKEND.entity.Marketing;

import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class QueryMarketing extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public QueryMarketing(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<Marketing> getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        System.err.println(count);
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<Marketing> marketingArrayList = new ArrayList<Marketing>();
        for (int i=0;i<jsonArray.length();i++){
            JSONArray data = jsonArray.getJSONArray(i);
            Marketing marketing = new Marketing();
            marketing.setId(data.getString(0));
            marketing.setUnit(data.getString(1));
            marketing.setName(data.getString(2));
            marketing.setDescriptio(data.getString(3));
            marketing.setNO(data.getString(4));
            marketing.setNum(data.getString(5));
            marketingArrayList.add(marketing);
        }
        //排序
        try {
            Collections.sort(marketingArrayList, new SortByMarketing());
        } catch (Exception e) {

        }

        return marketingArrayList;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");

        columns.add("UNIT");//
        columns.add("NAME");//
        columns.add("DESCRIPTION");//
        columns.add("NO");
        columns.add("TOT_QTY");

        params.put("columns", columns);
        params.put("table", "YC_LABELDEFINE");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'ISACTIVE'");
        paramss.put("condition", "Y");
        params.put("params", paramss);

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "DESCRIPTION");
        orderby.put("asc", true);
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QueryMarketing queryMail = new QueryMarketing("39.99.239.133:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(queryMail.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class SortByMarketing implements Comparator {
        public int compare(Object o1, Object o2) {
            Marketing s1 = (Marketing) o1;
            Marketing s2 = (Marketing) o2;

            if (Integer.parseInt(s1.getDescriptio()) > Integer.parseInt(s2.getDescriptio()))
                return 1;
            return -1;
            //不用写s1.getAge()==s2.getAge()的情况
        }

    }

}
